//
//  SetUserInfoViewController.swift
//  DiDiRecycle
//
//  Created by abb on 16/7/30.
//  Copyright © 2016年 abb. All rights reserved.
//

import UIKit
import SVProgressHUD
import IQKeyboardManagerSwift
class SetUserInfoViewController: AbbBaseViewController {
    /// 用户模型
    var user:User!
    /// 用户昵称
    var nickName:String?
    /// 密码
    var password:String?
    /// 重复密码
    var repeatPassword:String?
    // navBar透明
    override var userTransparentNavigationBar: Bool {
        return true
    }
    init(userModel:User) {
        super.init(nibName: nil, bundle: nil)
        user = userModel
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        SVProgressHUD.setMinimumDismissTimeInterval(1)
    }
    
    func saveUserInfo() {
        // 键盘消失
        IQKeyboardManager.sharedManager().resignFirstResponder()
        // 判断密码和重复密码
        if password != repeatPassword {
            SVProgressHUD.showErrorWithStatus("两次密码不一致!")
            return
        }
        SVProgressHUD.showWithStatus("请稍等...")
        // 网络请求
        UserDAO.setUserInfo(user.phoneNumber!
            , nickName: nickName!, password: password!) { (bool, result) in
                if bool {
                    // 设置完成就归档
                    self.user.nickName = self.nickName
                    self.user.password = self.password
                    if self.user.saveAccount() {
                        SVProgressHUD.dismiss()
                        NSNotificationCenter.defaultCenter().postNotificationName(ABBRootViewControllerKey, object: true)
                    } else {
                        SVProgressHUD.showErrorWithStatus("保存用户失败")
                    }
                } else {
                    SVProgressHUD.showErrorWithStatus(result)
                }
        }
    }
    
    private func setupUI() {
        // 添加子控件
        view.addSubview(titleLabel)
        view.addSubview(nickNameView)
        view.addSubview(passwordView)
        view.addSubview(repeatPasswordView)
        view.addSubview(saveBtn)
        // 布局子控件
        titleLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.width.equalTo(self.view).multipliedBy(0.83)
            make.centerY.equalTo(self.view).multipliedBy(0.5)
        }
        nickNameView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.width.equalTo(self.view).multipliedBy(0.83)
            make.top.equalTo(titleLabel.snp_bottom).offset(40)
        }
        passwordView.snp_makeConstraints { (make) in
            make.top.equalTo(nickNameView.snp_bottom).offset(20)
            make.width.equalTo(self.view).multipliedBy(0.83)
            make.centerX.equalTo(self.view)
        }
        repeatPasswordView.snp_makeConstraints { (make) in
            make.top.equalTo(passwordView.snp_bottom).offset(20)
            make.width.equalTo(self.view).multipliedBy(0.83)
            make.centerX.equalTo(self.view)
        }
        saveBtn.snp_makeConstraints { (make) in
            make.top.equalTo(repeatPasswordView.snp_bottom).offset(40)
            make.width.equalTo(self.view).multipliedBy(0.83)
            make.centerX.equalTo(self.view)
            make.height.equalTo(42)
        }
        // 监听按钮
        saveBtn.addTarget(self, action: #selector(saveUserInfo), forControlEvents: .TouchUpInside)
        
    }
    // MARK: - 懒加载
    private lazy var titleLabel:UILabel = {
        let label = UILabel()
        label.text = "请完善你的信息"
        label.font = UIFont.systemFontOfSize(20)
        label.textAlignment = NSTextAlignment.Center
        return label
    }()
    /// 昵称输入框
    private lazy var nickNameView: FormsView = {
        let fv = FormsView(CallBack: { (text) in
            self.nickName = text
        })
        fv.textField.placeholder = "昵称"
        fv.btn.setImage(UIImage(named:"nickname"), forState: .Normal)
        return fv
    }()
    /// 密码
    private lazy var passwordView: FormsView = {
        let fv = FormsView(CallBack: { (text) in
            self.password = text
        })
        fv.textField.placeholder = "请输入密码"
        fv.btn.setImage(UIImage(named:"password"), forState: .Normal)
        return fv
    }()
    /// 重复密码
    private lazy var repeatPasswordView: FormsView = {
        let fv = FormsView(CallBack: { (text) in
            self.repeatPassword = text
        })
        fv.textField.placeholder = "请再次输入密码"
        fv.btn.setImage(UIImage(named:"password"), forState: .Normal)
        return fv
    }()
    /// 登录按钮
    private lazy var saveBtn: UIButton = UIButton.buttonWithColor("保存")

}
